package top.zywork.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.common.StringUtils;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.ActivityRanksDTO;
import top.zywork.dto.RanksUserDTO;
import top.zywork.query.ActivityRanksQuery;
import top.zywork.query.RanksUserQuery;
import top.zywork.service.ActivityRanksService;
import top.zywork.service.ActivityService;
import top.zywork.service.RanksUserService;
import top.zywork.vo.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ActivityRanksController控制器类<br/>
 *
 * 创建于2020-05-13<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@RestController
@RequestMapping("/activity-ranks")
@Slf4j
public class ActivityRanksController extends BaseController {

    private ActivityRanksService activityRanksService;

    private RanksUserService ranksUserService;

    private ActivityService activityService;

    @PostMapping("admin/save")
    @SysLog(description = "后台保存ActivityRanksVO", requestParams = false)
    public ResponseStatusVO save(@RequestBody @Validated ActivityRanksVO activityRanksVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityRanksService.save(BeanUtils.copy(activityRanksVO, ActivityRanksDTO.class));
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存ActivityRanksVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<ActivityRanksVO> activityRanksVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityRanksService.saveBatch(BeanUtils.copyListObj(activityRanksVOList, ActivityRanksDTO.class));
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除ActivityRanksVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        activityRanksService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除ActivityRanksVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        activityRanksService.removeByIds(StringUtils.strArrayToLongArray(ids));
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新ActivityRanksVO", requestParams = false)
    public ResponseStatusVO update(@RequestBody @Validated ActivityRanksVO activityRanksVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        int updateRows = activityRanksService.update(BeanUtils.copy(activityRanksVO, ActivityRanksDTO.class));
        if (updateRows == 1) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新ActivityRanksVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<ActivityRanksVO> activityRanksVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        activityRanksService.updateBatch(BeanUtils.copyListObj(activityRanksVOList, ActivityRanksDTO.class));
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结ActivityRanksVO")
    public ResponseStatusVO active(@RequestBody ActivityRanksVO activityRanksVO) {
        activityRanksService.update(BeanUtils.copy(activityRanksVO, ActivityRanksDTO.class));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结ActivityRanksVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<ActivityRanksVO> activityRanksVOList) {
        activityRanksService.updateBatch(BeanUtils.copyListObj(activityRanksVOList, ActivityRanksDTO.class));
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        ActivityRanksVO activityRanksVO = new ActivityRanksVO();
        Object obj = activityRanksService.getById(id);
        if (obj != null) {
            activityRanksVO = BeanUtils.copy(obj, ActivityRanksVO.class);
        }
        return ResponseStatusVO.ok("查询成功", activityRanksVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        PagerDTO pagerDTO = activityRanksService.listAll();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityRanksVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody ActivityRanksQuery activityRanksQuery) {
        PagerDTO pagerDTO = activityRanksService.listAllByCondition(activityRanksQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityRanksVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody ActivityRanksQuery activityRanksQuery) {
        PagerDTO pagerDTO = activityRanksService.listPageByCondition(activityRanksQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), ActivityRanksVO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    /**
     * 根据活动编号查询活动队伍信息
     * @param activityId
     */
    @GetMapping("any/all-by-activity/{activityId}")
    public ResponseStatusVO anyAllByActivyId(@PathVariable("activityId") Long activityId) {
        ActivityRanksQuery activityRanksQuery = ActivityRanksQuery.builder().activityId(activityId).build();
        List<Object> objectList = activityRanksService.listAllByCondition(activityRanksQuery).getRows();
        List<ActivityRanksVO> activityRanksVOList = BeanUtils.copy(objectList, ActivityRanksVO.class);
        List<ActivityRanksVO> newActivityRanksVOList = new ArrayList<>();
        for (ActivityRanksVO activityRanksVO : activityRanksVOList) {
            RanksUserQuery ranksUserQuery = RanksUserQuery.builder().activityRanksId(activityRanksVO.getId()).build();
            ranksUserQuery.setSortColumn("position");
            List<Object> ranksUserObjectList = ranksUserService.listAllByCondition(ranksUserQuery).getRows();
            List<RanksUserVO> ranksUserVOList = BeanUtils.copy(ranksUserObjectList, RanksUserVO.class);
            activityRanksVO.setRanksUserList(ranksUserVOList);
            newActivityRanksVOList.add(activityRanksVO);
        }
        return ResponseStatusVO.ok("查询成功", newActivityRanksVOList);
    }


    /**
     * 更新队伍获胜方
     * @param activityId
     * @param type
     * @return
     */
    @PostMapping("user/update-victory")
    public ResponseStatusVO anyUpateVictory(Long activityId, String type) {
        ActivityRanksQuery activityRanksQuery = ActivityRanksQuery.builder().activityId(activityId).build();
        List<Object> objectList = activityRanksService.listAllByCondition(activityRanksQuery).getRows();
        List<ActivityRanksVO> activityRanksVOList = BeanUtils.copy(objectList, ActivityRanksVO.class);
        ActivityRanksVO blueRanks = new ActivityRanksVO();
        ActivityRanksVO redRanks = new ActivityRanksVO();
        for (ActivityRanksVO activityRanksVO : activityRanksVOList) {
            if (activityRanksVO.getType().equals("蓝方")) {
                blueRanks = activityRanksVO;
            } else if (activityRanksVO.getType().equals("红方")) {
                redRanks = activityRanksVO;
            }
        }
        if (blueRanks.getType().equals(type)) {
            // 蓝方胜
            blueRanks.setIsVictory((byte)1);
            redRanks.setIsVictory((byte)0);
        } else {
            // 红方胜
            redRanks.setIsVictory((byte)1);
            blueRanks.setIsVictory((byte)0);
        }
        List<ActivityRanksVO> updateList = new ArrayList<>();
        updateList.add(blueRanks);
        updateList.add(redRanks);
        activityRanksService.updateBatch(BeanUtils.copyListObj(updateList, ActivityRanksVO.class));
        // 将活动改成已结束
        ActivityVO activityVO = ActivityVO.builder().id(activityId).status("已结束").build();
        activityService.update(activityVO);
        return ResponseStatusVO.ok("更新成功", null);
    }

    @Autowired
    public void setActivityRanksService(ActivityRanksService activityRanksService) {
        this.activityRanksService = activityRanksService;
    }

    @Autowired
    public void setRanksUserService(RanksUserService ranksUserService) {
        this.ranksUserService = ranksUserService;
    }

    @Autowired
    public void setActivityService(ActivityService activityService) {
        this.activityService = activityService;
    }
}